package com.atguigu.ssyx.payment.service.impl;

import com.atguigu.ssyx.common.exception.SsyxException;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import com.atguigu.ssyx.enums.PaymentStatus;
import com.atguigu.ssyx.enums.PaymentType;
import com.atguigu.ssyx.model.order.OrderInfo;
import com.atguigu.ssyx.model.order.PaymentInfo;
import com.atguigu.ssyx.mq.constant.MqConst;
import com.atguigu.ssyx.mq.service.RabbitService;
import com.atguigu.ssyx.order.client.OrderFeignClient;
import com.atguigu.ssyx.payment.mapper.PaymentMapper;
import com.atguigu.ssyx.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * ClassName: PaymentInfoServiceImpl
 * Package: com.atguigu.ssyx.payment.service.impl
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-17
 * Time: 15:50
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private RabbitService rabbitService;

    //根据orderNo查询支付记录
    @Override
    public PaymentInfo getPaymentInfoByOrderNo(String orderNo) {

        PaymentInfo paymentInfo = baseMapper.selectOne(
                new LambdaQueryWrapper<PaymentInfo>()
                        .eq(PaymentInfo::getOrderNo, orderNo)
        );
        return paymentInfo;
    }

    //添加支付记录
    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        //远程调用 orderNo查询订单信息
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderNo);
        if (orderInfo == null) {
            throw new SsyxException(ResultCodeEnum.DATA_ERROR);
        }
        //封装到PaymentInfo对象中
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(PaymentType.WEIXIN);
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setOrderNo(orderInfo.getOrderNo());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID);
        String subject = "UserId:" + orderInfo.getUserId() + "下订单";
        paymentInfo.setSubject(subject); //概要信息
        //paymentInfo.setTotalAmount(order.getTotalAmount());
        //支付数据 测试统一支付多少钱
        paymentInfo.setTotalAmount(new BigDecimal("0.01"));

        baseMapper.insert(paymentInfo);

        //调用方法实现添加
        return paymentInfo;

    }


    //支付成功 修改支付记录表状态 已经支付
    //支付成功 修改订单记录已经支付 库存扣减
    @Override
    public void paySuccess(String orderNo, Map<String, String> reslutMap) {
        //1.查询订单支付记录表状态是否是已经支付
        PaymentInfo paymentInfo = baseMapper.selectOne(
                new LambdaQueryWrapper<PaymentInfo>()
                        .eq(PaymentInfo::getOrderNo, orderNo)
        );

        if (paymentInfo.getPaymentStatus() != PaymentStatus.UNPAID){
            return;
        }
        //2 如果支付记录表状态没有支付 更新
        paymentInfo.setPaymentStatus(PaymentStatus.PAID);
        paymentInfo.setTradeNo(reslutMap.get("transaction_id"));
        paymentInfo.setCallbackContent(reslutMap.toString());
        baseMapper.updateById(paymentInfo);


        //3 修改订单记录已经支付 库存减扣 整合RabbitMQ实现
        rabbitService.sendMessage(MqConst.EXCHANGE_PAY_DIRECT,
                MqConst.ROUTING_PAY_SUCCESS,orderNo);
        //发送之后就是接收端


    }
}
